package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasurestepreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 国庆寻宝奖励领取
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayLotteryReward
 * @Author jingtao-zheng
 * @Date 2022/9/22 10:12
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayLotteryReward, classtype = NationalDayMessage.NationalDayLotteryRewardRequest.class)
public class CMD_Active_国庆寻宝_NationalDayLotteryReward extends Command<NationalDayMessage.NationalDayLotteryRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayLotteryRewardRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayLotteryRewardResponse.Builder response = NationalDayMessage.NationalDayLotteryRewardResponse.newBuilder();
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
            if (am.tryLock()) {
                try {
                    //1.获取 当前层数  已领取层数  层奖励信息
                    int floor = am.getFloor();
                    ArrayList<Integer> floorAward = am.getFloorAward();
                    Map<String, acttreasurestepreward> allData = DataCenter.getAllData(acttreasurestepreward.class);
                    //2.过滤能获取的奖励
                    List<acttreasurestepreward> collect = allData.values().stream().filter(_reward -> _reward.getStep() <= floor).sorted(Comparator.comparing(acttreasurestepreward::getStep)).collect(Collectors.toList());
                    for (yxy.cherry.data.bean.acttreasurestepreward acttreasurestepreward : collect) {
                        if (floorAward.contains(acttreasurestepreward.getId())) {
                            continue;
                        }else{
                            floorAward.add(acttreasurestepreward.getId());
                            Collection<Currency> currencies = Currency.create(acttreasurestepreward.getRewardtype(), acttreasurestepreward.getRewardid(), acttreasurestepreward.getRewardnum());
                            //状态前置
                            am.set();
                            //返回奖励物品给前端
                            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(currencies);
                            response.addAllReward(items);
                            //发奖
                            new CurrencyHandle().addCurrency(playercode, CCReason.国庆寻宝, currencies);
                            //领取的奖励信息
                            Map<String, yxy.cherry.data.bean.acttreasurestepreward> allrewardData = DataCenter.getAllData(yxy.cherry.data.bean.acttreasurestepreward.class);
                            for (yxy.cherry.data.bean.acttreasurestepreward value : allrewardData.values()) {
                                NationalDayMessage.NationalDayLotteryReward.Builder rwds = NationalDayMessage.NationalDayLotteryReward.newBuilder();
                                rwds.setId(value.getId());//表id
                                Collection<Currency> currenciesreward = Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum());
                                Collection<PackageMessage.Item> itemsreward = new CurrencyHandle().packItems(currenciesreward);
                                rwds.addAllRewards(itemsreward);//奖品
                                rwds.setFloor(value.getStep());//对应层数
                                ArrayList<Integer> floorAwardreward = am.getFloorAward();
                                boolean contains = floorAwardreward.contains(value.getId());
                                rwds.setIsGot(contains);//是否已领取
                                rwds.setType(value.getRewardlv());
                                response.addRwds(rwds);
                            }
                            break;
                        }
                    }
                    send(messageSource, response, Result.Success);
                } catch (Exception e) {
                    e.printStackTrace();
                    send(messageSource, response, Result.DataError);
                } finally {
                    am.unlock();
                }
            } else {
                send(messageSource, response, Result.DataError);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayLotteryRewardResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
